package cn.duhengkui.national.valuelist.gen;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cn.duhengkui.national.valuelist.core.entity.BaseEntity;
import cn.duhengkui.national.valuelist.tool.ClassPathScanHandler;
import cn.duhengkui.national.valuelist.tool.EntityData;

/**
 * duhengkui
 * 创建entity和dao工具类
 */
public class CreateUtil {
	
	public static void writeFile(String path, String fileName, StringBuffer code) {
		File file = new File(path);
		if(!file.exists()) {
			file.mkdirs();
		}
		file = new File(path + File.separator + fileName);
		if(file.exists()) {
			file.delete();
		}
		FileWriter fw = null;
		try {
			fw = new FileWriter(file);
			fw.write(code.toString());
			fw.flush();
			fw.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	
	public static void createEntitys(DataBaseInfo dataBaseInfo, String packageName, String sourcePath, String entityNamesPattern, boolean isUpperEntityName) {
		List<EntityData> list = dataBaseInfo.entityList(isUpperEntityName, entityNamesPattern.split(","));
		EntityFactory factory = dataBaseInfo.getEntityFactory();
		for(EntityData entity : list) {
			factory.createEntityFile(packageName, sourcePath, entity, dataBaseInfo);
		}
	}
	
	public static void createDaos(String daoPackage, String sourcePath,String entityPackage,boolean readOnly){
		ClassPathScanHandler scanHandler = new ClassPathScanHandler(true, true, new ArrayList<String>());
		Set<Class<?>> classSet = scanHandler.getPackageAllClasses(entityPackage, true,false);
		DaoFactory factory = new DaoFactoryImpl();
		for(Class<?> clazz : classSet) {
			if(clazz.getSuperclass().equals(BaseEntity.class)) {
				@SuppressWarnings("unchecked")
				Class<? extends BaseEntity> relClazz =  (Class<? extends BaseEntity>)clazz;
				factory.createDaoFile(daoPackage, sourcePath, relClazz, readOnly);
			}
		}
	}
	
	public static String fieldName(String str) {
		return firstLower(className(str));
	}
	
	public static String className(String str) {
		String[] strs = str.split("_");
		StringBuffer result = new StringBuffer();
		for(String name : strs) {
			result.append(fristUpper(name.toLowerCase()));
		}
		return result.toString();
	}
	
	public static String getterName(String str) {
		return "get" + fristUpper(fieldName(str));
	}
	
	public static String setterName(String str) {
		return "set" + fristUpper(fieldName(str));
	}
	
	public static String fristUpper(String str) {
		return str.substring(0, 1).toUpperCase() + str.substring(1);
	}
	
	public static String firstLower(String str) {
		return str.substring(0, 1).toLowerCase() + str.substring(1);
	}
	
	public static String defaultValue(int type, String value) {
		if(value == null) {
			return "";
		}
		switch (type) {
		case Types.CHAR:
		case Types.NCHAR:
		case Types.LONGVARCHAR:
		case Types.LONGNVARCHAR:
		case Types.VARCHAR:
		case Types.NVARCHAR:
		return " = \"" + value + "\"";
		default:
			return "";
		}
	}

}
